<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;


class Emailinform extends Backend
{
    protected $model = null;
    protected $dataLimit = 'personal';
    protected $dataLimitField = 'uid';
    protected $multiFields = 'instoragesucc,instoragefail,outstoragesucc,outstoragefail';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\user\email\Inform;

    }

    public function index()
    {
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            //第一次数据初始化
            if(empty($list)){
                $data[$this->dataLimitField] = $this->auth->aid;
                $this->model->save($data);
            }
            $list = collection($list)->toArray();
            $result = array("total" => 0, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * @param string $ids
     * @param $email
     */
    public function saveEmail($ids = "",$email)
    {
        $ids = $ids ? $ids : $this->request->param("ids");
        $email = $email ? $email : $this->request->param("email");

        if ($ids) {
            $data['email'] = $email;
            $data['id'] = $ids;
            $rs = $this->model->isUpdate(true)->save($data);

            if ($rs) {
                $this->success();
            } else {
                $this->error(__('No rows were updated'));
            }
        }else{
        $this->error(__('Parameter %s can not be empty', 'ids'));
        }

    }

    

}
